package com.adapter;

import java.util.ArrayList;
import java.util.List;
import java.util.Vector;

import com.entity.YoukuVideo;
import com.videoplayer.R;


import android.content.Context;
import android.graphics.drawable.Drawable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

public class BookItemAdapter extends BaseAdapter{
	
	private Context c;
	private List<YoukuVideo> mModels = new ArrayList<YoukuVideo>();
	private ListView mListView;
	SyncImageLoader syncImageLoader;
	
	public BookItemAdapter(Context context,ListView listView,List<YoukuVideo> YoukuVideos){
		this.c = context;
		syncImageLoader = new SyncImageLoader();
		mListView = listView;
		mModels = YoukuVideos;
		mListView.setOnScrollListener(onScrollListener);
	}

	
	
	public void clean(){
		mModels.clear();
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return mModels.size();
	}

	@Override
	public Object getItem(int position) {
		if(position >= getCount()){
			return null;
		}
		return mModels.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		if(convertView == null){
			convertView = View.inflate(c,R.layout.book_item_adapter, null);
		}
		YoukuVideo model = mModels.get(position);
		convertView.setTag(position);
		ImageView iv = (ImageView) convertView.findViewById(R.id.sItemIcon);
		TextView sItemTitle =  (TextView) convertView.findViewById(R.id.sItemTitle);
		TextView sItemInfo =  (TextView) convertView.findViewById(R.id.sItemInfo);
		TextView sTime = (TextView) convertView.findViewById(R.id.tv_time);
	             convertView.findViewById(R.id.sItemTitle)  
	             .setSelected(true);  
	  
		sItemTitle.setText(model.getvTitle());
		sItemInfo.setText(model.getvClarity());
			sTime.setVisibility(View.VISIBLE);
			sTime.setText(model.getvTime());
		iv.setBackgroundResource(R.drawable.detail_pic_loading);
		syncImageLoader.loadImage(position,model.getvImg(),imageLoadListener);
		return  convertView;
	}

	SyncImageLoader.OnImageLoadListener imageLoadListener = new SyncImageLoader.OnImageLoadListener(){

		@Override
		public void onImageLoad(Integer t, Drawable drawable) {
			//BookModel model = (BookModel) getItem(t);
			View view = mListView.findViewWithTag(t);
			if(view != null){
				ImageView iv = (ImageView) view.findViewById(R.id.sItemIcon);
				iv.setBackgroundDrawable(drawable);
			}
		}
		@Override
		public void onError(Integer t) {
			YoukuVideo model = (YoukuVideo) getItem(t);
			View view = mListView.findViewWithTag(model);
			if(view != null){
				ImageView iv = (ImageView) view.findViewById(R.id.sItemIcon);
				iv.setBackgroundResource(R.drawable.detail_pic_loading);
			}
		}
		
	};
	
	public void loadImage(){
		int start = mListView.getFirstVisiblePosition();
		int end =mListView.getLastVisiblePosition();
//		if(end >= getCount()){
//			end = getCount() -1;
//		}
		syncImageLoader.setLoadLimit(start-6<0?0:start-6, end+6>= getCount()?getCount()-1:end+6);
		syncImageLoader.unlock();
	}
	
	AbsListView.OnScrollListener onScrollListener = new AbsListView.OnScrollListener() {
		
		@Override
		public void onScrollStateChanged(AbsListView view, int scrollState) {
			switch (scrollState) {
		/*	
				case AbsListView.OnScrollListener.SCROLL_STATE_FLING:
					syncImageLoader.lock();
//					Log.v("scroll","SCROLL_STATE_FLING");
					break;
				case AbsListView.OnScrollListener.SCROLL_STATE_IDLE:
				//	syncImageLoader.lock();
					//loadImage();
//					Log.v("scroll","SCROLL_STATE_IDLE");
//					break;
				case AbsListView.OnScrollListener.SCROLL_STATE_TOUCH_SCROLL:
					
					//syncImageLoader.lock();
					loadImage();
				//	loadImage();
				//	syncImageLoader.lock();
//					Log.v("scroll","SCROLL_STATE_TOUCH_SCROLL");
					break;
	
				default:
					break;
			*/		
			}
			
		}
		
		@Override
		public void onScroll(AbsListView view, int firstVisibleItem,
				int visibleItemCount, int totalItemCount) {
			// TODO Auto-generated method stub
			
		}
	};
}
